﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 按钮角色仓储
    /// </summary>
    public class SysButtonRoleRepository : Repository<SysButtonRole>, ISysButtonRoleRepository
    {

        /// <summary>
        /// 根据角色获取菜单和按钮关系
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <returns></returns>
        public List<SysButtonRole> GetButtonRoleByRoleId(List<int> roleId)
        {
            string sql = @"   select sr.* from  Sys_ButtonRole as sr
                             inner join Sys_Menu as sm  on sr.MenuId = sm.Id
                              where sr.RoleId in @Key ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonRole>(sql, new { Key = roleId.ToArray() }).ToList();
        }

        /// <summary>
        /// 根据角色id删除数据
        /// </summary>
        /// <param name="roleId">角色id</param>
        public void DeleteByRoleId(int roleId)
        {
            string sql = " delete from  Sys_ButtonRole where RoleId=@RoleId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { RoleId = roleId });
        }

        /// <summary>
        /// 新增按钮和角色关系
        /// </summary>
        /// <param name="list">新增数据</param>
        public void BatchInsert(List<SysButtonRole> list)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(BaseSqlRepository.sysButtonRole_insertSql, list);
        }

        /// <summary>
        /// 根据角色id和菜单id获取按钮
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="menuId">菜单id</param>
        /// <returns>返回对应按钮集合</returns>
        public List<SysButtonRole> GetButtonByRoleIdOrMenuId(List<int> roleId, string menuId)
        {
            string sql = @"select  * from  Sys_ButtonRole where  MenuId = @MenuId and  RoleId in @RoleId";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysButtonRole>(sql, new { MenuId = menuId, RoleId = roleId }).ToList();
        }
    }
}
